package com.example.demo.util;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

import java.util.Scanner;

/**
 * @author gjy
 * @ClassName JasyptUtil
 * @description jasypt3.x 加密工具类 用于加密、解密验证
 * @date 2025/5/15 16:41
 */
public class JasyptUtil {


    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {

            System.out.println("请输入字符串：");
            String plainText = scanner.nextLine();  // 读取整行输入[2,4](@ref)
            if ("exit".equals(plainText)) {
                scanner.close();
                break;
            }
            System.out.println("输入加密盐：");
            String salt = scanner.nextLine();    // 读取整数[4,7](@ref)
            String encryptWithSHA512Str = encryptWithSHA512(plainText, salt);
            String decryptWithSHA512Str = decryptWithSHA512(encryptWithSHA512Str, salt);
            System.out.println("加密前：" + plainText);
            System.out.println("加密后3.x：" + encryptWithSHA512Str);
            System.out.println("解密后3.x：" + decryptWithSHA512Str);
            System.out.println("复制到配置文件：ENC(" + encryptWithSHA512Str + ")");

        }
    }

    /**
     * Jasyp3.x 加密（PBEWITHHMACSHA512ANDAES_256）
     *
     * @param plainText 待加密的原文
     * @param factor    加密秘钥
     * @return java.lang.String
     */
    public static String encryptWithSHA512(String plainText, String factor) {
        // 1. 创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写，以下都是 Jasyp 3.x 版本，配置文件默认配置
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 3. 加密
        return encryptor.encrypt(plainText);
    }

    /**
     * Jaspy3.x 解密（PBEWITHHMACSHA512ANDAES_256）
     *
     * @param encryptedText 待解密密文
     * @param factor        解密秘钥
     * @return java.lang.String
     */
    public static String decryptWithSHA512(String encryptedText, String factor) {
        // 1. 创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写，以下都是 Jasyp 3.x 版本，配置文件默认配置
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 3. 解密
        return encryptor.decrypt(encryptedText);
    }


}
